package com.open.party.cloud.view.home.juBao

import android.os.Bundle
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.JuBaoChaXunBean
import com.open.party.cloud.viewModel.BizViewModel
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.activity_jbpt_fan_kui_cha_xun.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class JuBao12380ChaXunSearchActivity : AppEventBusTitleBaseActivity() {

    var viewModel: BizViewModel? = null

    override fun getLayoutResId(): Int = R.layout.activity_jbpt_fan_kui_cha_xun

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("反馈查询")

        viewModel = BizViewModel()

        submitBtn.setOnClickListener {
            val codeStr = juBaoBianHaoEt.text.toString()

            if (XUtils.string().isEmpty(codeStr)) {
                XUtils.toast().show("请输入举报编码")
                return@setOnClickListener
            }

            if (!XUtils.net().isAvailable) {
                XUtils.toast().show("当前网络不可用")
                return@setOnClickListener
            }
            showLoadingDialog("提交中")
            viewModel!!.getReplyByCode(codeStr)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<JuBaoChaXunBean>) {
        if (result.eventType != "getReplyByCode") {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    XUtils.intent().openActivity(this, JuBao12380ChaXunResultActivity::class.java)
                        .putSerializableExtra("result", result.data)
                        .start()
                } else {
                    XUtils.toast().show("查无结果")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }
}